From 5aae6f09b31130e826572e685f9118d3edc81fe6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Oct 2016 14:39:46 +0300 Subject: [PATCH] Remove command prototype --- src/cargo/ops/cargo_run.rs | 3 +- src/cargo/ops/cargo_rustc/command.rs | 89 ----------------------- src/cargo/ops/cargo_rustc/compilation.rs | 35 ++++++--- src/cargo/ops/cargo_rustc/custom_build.rs | 49 ++++++------- src/cargo/ops/cargo_rustc/job_queue.rs | 5 +- src/cargo/ops/cargo_rustc/mod.rs | 31 ++++---- src/cargo/ops/cargo_test.rs | 4 +- src/cargo/ops/mod.rs | 1 - 8 files changed, 68 insertions(+), 149 deletions(-) delete mode 100644 src/cargo/ops/cargo_rustc/command.rs diff --git a/src/cargo/ops/cargo_run.rs b/src/cargo/ops/cargo_run.rs index f994541cc..34c28dbc4 100644 --- a/src/cargo/ops/cargo_run.rs +++ b/src/cargo/ops/cargo_run.rs @@ -48,8 +48,7 @@ pub fn run(ws: &Workspace, Some(path) => path.to_path_buf(), None => exe.to_path_buf(), }; - let mut process = try!(compile.target_process(exe, &root)) - .into_process_builder(); + let mut process = try!(compile.target_process(exe, &root)); process.args(args).cwd(config.cwd()); try!(config.shell().status("Running", process.to_string())); diff --git a/src/cargo/ops/cargo_rustc/command.rs b/src/cargo/ops/cargo_rustc/command.rs deleted file mode 100644 index d49951c8a..000000000 --- a/src/cargo/ops/cargo_rustc/command.rs +++ /dev/null @@ -1,89 +0,0 @@ -use std::collections::HashMap; -use std::ffi::{OsStr, OsString}; -use std::fmt; -use std::path::Path; - -use util::{CargoResult, ProcessBuilder, process}; -use util::Config; - -/// Prototype for a command that must be executed. -#[derive(Clone)] -pub struct CommandPrototype { - ty: CommandType, - builder: ProcessBuilder, -} - -impl CommandPrototype { - pub fn new(ty: CommandType, config: &Config) - -> CargoResult { - Ok(CommandPrototype { - builder: { - let mut p = match ty { - CommandType::Rustc => try!(config.rustc()).process(), - CommandType::Rustdoc => process(&*try!(config.rustdoc())), - CommandType::Target(ref s) | - CommandType::Host(ref s) => process(s), - }; - p.cwd(config.cwd()); - p - }, - ty: ty, - }) - } - - pub fn get_type(&self) -> &CommandType { &self.ty } - - pub fn arg>(&mut self, arg: T) -> &mut CommandPrototype { - self.builder.arg(arg); - self - } - - pub fn args>(&mut self, arguments: &[T]) -> &mut CommandPrototype { - self.builder.args(arguments); - self - } - - pub fn cwd>(&mut self, path: T) -> &mut CommandPrototype { - self.builder.cwd(path); - self - } - - pub fn env>(&mut self, key: &str, val: T) - -> &mut CommandPrototype { - self.builder.env(key, val); - self - } - - pub fn get_args(&self) -> &[OsString] { self.builder.get_args() } - pub fn get_cwd(&self) -> Option<&Path> { self.builder.get_cwd() } - - pub fn get_env(&self, var: &str) -> Option { - self.builder.get_env(var) - } - - pub fn get_envs(&self) -> &HashMap> { - self.builder.get_envs() - } - - pub fn into_process_builder(self) -> ProcessBuilder { - self.builder - } -} - -impl fmt::Display for CommandPrototype { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.builder.fmt(f) - } -} - -#[derive(Clone, Debug)] -pub enum CommandType { - Rustc, - Rustdoc, - - /// The command is to be executed for the target architecture. - Target(OsString), - - /// The command is to be executed for the host architecture. - Host(OsString), -} diff --git a/src/cargo/ops/cargo_rustc/compilation.rs b/src/cargo/ops/cargo_rustc/compilation.rs index 23f0a044f..0c6075169 100644 --- a/src/cargo/ops/cargo_rustc/compilation.rs +++ b/src/cargo/ops/cargo_rustc/compilation.rs @@ -1,12 +1,10 @@ use std::collections::{HashMap, HashSet}; -use std::ffi::OsStr; +use std::ffi::{OsStr, OsString}; use std::path::PathBuf; use semver::Version; use core::{PackageId, Package, Target}; -use util::{self, CargoResult, Config}; - -use super::{CommandType, CommandPrototype}; +use util::{self, CargoResult, Config, ProcessBuilder, process}; /// A structure returning the result of a compilation. pub struct Compilation<'cfg> { @@ -49,6 +47,18 @@ pub struct Compilation<'cfg> { config: &'cfg Config, } +#[derive(Clone, Debug)] +pub enum CommandType { + Rustc, + Rustdoc, + + /// The command is to be executed for the target architecture. + Target(OsString), + + /// The command is to be executed for the host architecture. + Host(OsString), +} + impl<'cfg> Compilation<'cfg> { pub fn new(config: &'cfg Config) -> Compilation<'cfg> { Compilation { @@ -67,25 +77,25 @@ impl<'cfg> Compilation<'cfg> { } /// See `process`. - pub fn rustc_process(&self, pkg: &Package) -> CargoResult { + pub fn rustc_process(&self, pkg: &Package) -> CargoResult { self.process(CommandType::Rustc, pkg) } /// See `process`. pub fn rustdoc_process(&self, pkg: &Package) - -> CargoResult { + -> CargoResult { self.process(CommandType::Rustdoc, pkg) } /// See `process`. pub fn target_process>(&self, cmd: T, pkg: &Package) - -> CargoResult { + -> CargoResult { self.process(CommandType::Target(cmd.as_ref().to_os_string()), pkg) } /// See `process`. pub fn host_process>(&self, cmd: T, pkg: &Package) - -> CargoResult { + -> CargoResult { self.process(CommandType::Host(cmd.as_ref().to_os_string()), pkg) } @@ -95,7 +105,7 @@ impl<'cfg> Compilation<'cfg> { /// The package argument is also used to configure environment variables as /// well as the working directory of the child process. pub fn process(&self, cmd: CommandType, pkg: &Package) - -> CargoResult { + -> CargoResult { let mut search_path = vec![]; // Add -L arguments, after stripping off prefixes like "native=" or "framework=". @@ -121,7 +131,12 @@ impl<'cfg> Compilation<'cfg> { search_path.extend(util::dylib_path().into_iter()); let search_path = try!(util::join_paths(&search_path, util::dylib_path_envvar())); - let mut cmd = try!(CommandPrototype::new(cmd, self.config)); + let mut cmd = match cmd { + CommandType::Rustc => try!(self.config.rustc()).process(), + CommandType::Rustdoc => process(&*try!(self.config.rustdoc())), + CommandType::Target(ref s) | + CommandType::Host(ref s) => process(s), + }; cmd.env(util::dylib_path_envvar(), &search_path); if let Some(env) = self.extra_env.get(pkg.package_id()) { for &(ref k, ref v) in env { diff --git a/src/cargo/ops/cargo_rustc/custom_build.rs b/src/cargo/ops/cargo_rustc/custom_build.rs index 5a15e85a8..73dd34e6d 100644 --- a/src/cargo/ops/cargo_rustc/custom_build.rs +++ b/src/cargo/ops/cargo_rustc/custom_build.rs @@ -10,7 +10,7 @@ use util::{internal, ChainError, profile, paths}; use super::job::Work; use super::{fingerprint, Kind, Context, Unit}; -use super::CommandType; +use super::compilation::CommandType; /// Contains the parsed output of a custom build script. #[derive(Clone, Debug, Hash)] @@ -98,30 +98,30 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) // package's library profile. let profile = cx.lib_profile(unit.pkg.package_id()); let to_exec = to_exec.into_os_string(); - let mut p = try!(super::process(CommandType::Host(to_exec), unit.pkg, cx)); - p.env("OUT_DIR", &build_output) - .env("CARGO_MANIFEST_DIR", unit.pkg.root()) - .env("NUM_JOBS", &cx.jobs().to_string()) - .env("TARGET", &match unit.kind { - Kind::Host => cx.host_triple(), - Kind::Target => cx.target_triple(), - }) - .env("DEBUG", &profile.debuginfo.to_string()) - .env("OPT_LEVEL", &profile.opt_level) - .env("PROFILE", if cx.build_config.release {"release"} else {"debug"}) - .env("HOST", cx.host_triple()) - .env("RUSTC", &try!(cx.config.rustc()).path) - .env("RUSTDOC", &*try!(cx.config.rustdoc())); - - if let Some(links) = unit.pkg.manifest().links(){ - p.env("CARGO_MANIFEST_LINKS", links); - } + let mut cmd = try!(super::process(CommandType::Host(to_exec), unit.pkg, cx)); + cmd.env("OUT_DIR", &build_output) + .env("CARGO_MANIFEST_DIR", unit.pkg.root()) + .env("NUM_JOBS", &cx.jobs().to_string()) + .env("TARGET", &match unit.kind { + Kind::Host => cx.host_triple(), + Kind::Target => cx.target_triple(), + }) + .env("DEBUG", &profile.debuginfo.to_string()) + .env("OPT_LEVEL", &profile.opt_level) + .env("PROFILE", if cx.build_config.release { "release" } else { "debug" }) + .env("HOST", cx.host_triple()) + .env("RUSTC", &try!(cx.config.rustc()).path) + .env("RUSTDOC", &*try!(cx.config.rustdoc())); + + if let Some(links) = unit.pkg.manifest().links() { + cmd.env("CARGO_MANIFEST_LINKS", links); + } // Be sure to pass along all enabled features for this package, this is the // last piece of statically known information that we have. if let Some(features) = cx.resolve.features(unit.pkg.package_id()) { for feat in features.iter() { - p.env(&format!("CARGO_FEATURE_{}", super::envify(feat)), "1"); + cmd.env(&format!("CARGO_FEATURE_{}", super::envify(feat)), "1"); } } @@ -192,19 +192,18 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) })); let data = &state.metadata; for &(ref key, ref value) in data.iter() { - p.env(&format!("DEP_{}_{}", super::envify(&name), - super::envify(key)), value); + cmd.env(&format!("DEP_{}_{}", super::envify(&name), + super::envify(key)), value); } } if let Some(build_scripts) = build_scripts { - try!(super::add_plugin_deps(&mut p, &build_state, + try!(super::add_plugin_deps(&mut cmd, &build_state, &build_scripts)); } } // And now finally, run the build command itself! - state.running(&p); - let cmd = p.into_process_builder(); + state.running(&cmd); let output = try!(cmd.exec_with_streaming( &mut |out_line| { state.stdout(out_line); Ok(()) }, &mut |err_line| { state.stderr(err_line); Ok(()) }, diff --git a/src/cargo/ops/cargo_rustc/job_queue.rs b/src/cargo/ops/cargo_rustc/job_queue.rs index d02d841b8..39d1c45a7 100644 --- a/src/cargo/ops/cargo_rustc/job_queue.rs +++ b/src/cargo/ops/cargo_rustc/job_queue.rs @@ -9,11 +9,10 @@ use term::color::YELLOW; use core::{PackageId, Target, Profile}; use util::{Config, DependencyQueue, Fresh, Dirty, Freshness}; -use util::{CargoResult, profile, internal}; +use util::{CargoResult, ProcessBuilder, profile, internal}; use super::{Context, Kind, Unit}; use super::job::Job; -use super::command::CommandPrototype; /// A management structure of the entire dependency graph to compile. /// @@ -64,7 +63,7 @@ enum Message { } impl<'a> JobState<'a> { - pub fn running(&self, cmd: &CommandPrototype) { + pub fn running(&self, cmd: &ProcessBuilder) { let _ = self.tx.send((self.key, Message::Run(cmd.to_string()))); } diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 1efcc2b0c..88328cb99 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -10,19 +10,17 @@ use rustc_serialize::json; use core::{Package, PackageId, PackageSet, Target, Resolve}; use core::{Profile, Profiles, Workspace}; use core::shell::ColorConfig; -use util::{self, CargoResult, human, machine_message}; +use util::{self, CargoResult, ProcessBuilder, human, machine_message}; use util::{Config, internal, ChainError, profile, join_paths, short_hash}; use self::job::{Job, Work}; use self::job_queue::JobQueue; -pub use self::compilation::Compilation; +pub use self::compilation::{Compilation, CommandType}; pub use self::context::{Context, Unit}; -pub use self::command::{CommandPrototype, CommandType}; pub use self::layout::{Layout, LayoutProxy}; pub use self::custom_build::{BuildOutput, BuildMap, BuildScripts}; -mod command; mod compilation; mod context; mod custom_build; @@ -270,9 +268,8 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult { } state.running(&rustc); - let process_builder = rustc.into_process_builder(); try!(if json_errors { - process_builder.exec_with_streaming( + rustc.exec_with_streaming( &mut |line| if !line.is_empty() { Err(internal(&format!("compiler stdout is not empty: `{}`", line))) } else { @@ -293,7 +290,7 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult { }, ).map(|_| ()) } else { - process_builder.exec() + rustc.exec() }.chain_error(|| { human(format!("Could not compile `{}`.", name)) })); @@ -359,7 +356,7 @@ fn rustc(cx: &mut Context, unit: &Unit) -> CargoResult { // Add all relevant -L and -l flags from dependencies (now calculated and // present in `state`) to the command provided - fn add_native_deps(rustc: &mut CommandPrototype, + fn add_native_deps(rustc: &mut ProcessBuilder, build_state: &BuildMap, build_scripts: &BuildScripts, pass_l_flag: bool, @@ -394,7 +391,7 @@ fn load_build_deps(cx: &Context, unit: &Unit) -> Option> { // For all plugin dependencies, add their -L paths (now calculated and // present in `state`) to the dynamic library load path for the command to // execute. -fn add_plugin_deps(rustc: &mut CommandPrototype, +fn add_plugin_deps(rustc: &mut ProcessBuilder, build_state: &BuildMap, build_scripts: &BuildScripts) -> CargoResult<()> { @@ -417,7 +414,7 @@ fn add_plugin_deps(rustc: &mut CommandPrototype, fn prepare_rustc(cx: &Context, crate_types: Vec<&str>, - unit: &Unit) -> CargoResult { + unit: &Unit) -> CargoResult { let mut base = try!(process(CommandType::Rustc, unit.pkg, cx)); build_base_args(cx, &mut base, unit, &crate_types); build_plugin_args(&mut base, cx, unit); @@ -474,7 +471,7 @@ fn rustdoc(cx: &mut Context, unit: &Unit) -> CargoResult { } } state.running(&rustdoc); - rustdoc.into_process_builder().exec().chain_error(|| { + rustdoc.exec().chain_error(|| { human(format!("Could not document `{}`.", name)) }) })) @@ -502,7 +499,7 @@ fn root_path(cx: &Context, unit: &Unit) -> PathBuf { } fn build_base_args(cx: &Context, - cmd: &mut CommandPrototype, + cmd: &mut ProcessBuilder, unit: &Unit, crate_types: &[&str]) { let Profile { @@ -616,8 +613,8 @@ fn build_base_args(cx: &Context, } -fn build_plugin_args(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit) { - fn opt(cmd: &mut CommandPrototype, key: &str, prefix: &str, +fn build_plugin_args(cmd: &mut ProcessBuilder, cx: &Context, unit: &Unit) { + fn opt(cmd: &mut ProcessBuilder, key: &str, prefix: &str, val: Option<&OsStr>) { if let Some(val) = val { let mut joined = OsString::from(prefix); @@ -637,7 +634,7 @@ fn build_plugin_args(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit) { opt(cmd, "-C", "linker=", cx.linker(unit.kind).map(|s| s.as_ref())); } -fn build_deps_args(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit) +fn build_deps_args(cmd: &mut ProcessBuilder, cx: &Context, unit: &Unit) -> CargoResult<()> { let layout = cx.layout(unit); cmd.arg("-L").arg(&{ @@ -658,7 +655,7 @@ fn build_deps_args(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit) return Ok(()); - fn link_to(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit) + fn link_to(cmd: &mut ProcessBuilder, cx: &Context, unit: &Unit) -> CargoResult<()> { for (filename, linkable) in try!(cx.target_filenames(unit)) { if !linkable { @@ -677,7 +674,7 @@ fn build_deps_args(cmd: &mut CommandPrototype, cx: &Context, unit: &Unit) } pub fn process(cmd: CommandType, pkg: &Package, - cx: &Context) -> CargoResult { + cx: &Context) -> CargoResult { // When invoking a tool, we need the *host* deps directory in the dynamic // library search path for plugins and such which have dynamic dependencies. let mut search_path = util::dylib_path(); diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index d73ec8103..71a22d4e4 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -98,7 +98,7 @@ fn run_unit_tests(options: &TestOptions, shell.status("Running", cmd.to_string()) })); - if let Err(e) = cmd.into_process_builder().exec() { + if let Err(e) = cmd.exec() { errors.push(e); if !options.no_fail_fast { break @@ -175,7 +175,7 @@ fn run_doc_tests(options: &TestOptions, try!(config.shell().verbose(|shell| { shell.status("Running", p.to_string()) })); - if let Err(e) = p.into_process_builder().exec() { + if let Err(e) = p.exec() { errors.push(e); if !options.no_fail_fast { return Ok(errors); diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index 4d646ea30..ff4583c56 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -5,7 +5,6 @@ pub use self::cargo_read_manifest::{read_manifest,read_package,read_packages}; pub use self::cargo_rustc::{compile_targets, Compilation, Layout, Kind, Unit}; pub use self::cargo_rustc::{Context, LayoutProxy}; pub use self::cargo_rustc::{BuildOutput, BuildConfig, TargetConfig}; -pub use self::cargo_rustc::{CommandType, CommandPrototype}; pub use self::cargo_run::run; pub use self::cargo_install::{install, install_list, uninstall}; pub use self::cargo_new::{new, init, NewOptions, VersionControl}; -- 2.30.2